package com.wannengbang.cloudleader.widget;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;


/**
 * Time: 2019/3/12 0012
 * Author: ZF
 * Description:检查网络状态切换 - 广播接受器
 * <p>
 * 在Android系统中，当网络状态改变的时候，
 * 系统会发送一个名为CONNECTIVITY_ACTION的广播，
 * 如果要监听网络状态变化，就需要对该广播进行监听。
 */
public class NetBroadcastReceiver extends BroadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) {
        // 如果相等的话就说明网络状态发生了变化
        if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
            ConnectivityManager cm = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = null;
            boolean connectedOrConnecting = false;

            if (cm != null) {
                networkInfo = cm.getActiveNetworkInfo();
            }
            if (networkInfo != null) {
                connectedOrConnecting = networkInfo.isConnectedOrConnecting();
            }

//            EventBus.getDefault().post(new NetChangedBean(connectedOrConnecting));
        }
    }
}  